package com.breakfast.fun.adapter.home;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.breakfast.fun.R;
import com.breakfast.fun.base.BaseListAdapter;
import com.breakfast.fun.common.CommonCart;
import com.breakfast.fun.dao.ProductCart;
import com.breakfast.fun.data.entity.home.Product;
import com.breakfast.fun.ui.home.product.FreeProductActivity;
import com.breakfast.fun.ui.user.cart.UserCartActivity;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 一元购分类产品适配器
 * Created by Administrator on 2016/9/7.
 */
public class FreeProductAdapter extends BaseListAdapter<Product> {

    private CommonCart mCommonCart = new CommonCart();

    public FreeProductAdapter(Context context, List<Product> list) {
        super(context, list);

        mCommonCart.init(context);
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_free_product, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Product data = getItem(i);
        imageUtil.loadUrlImage(data.getImage(), holder.mIvFreeProduct);
        holder.mTvFreeProductName.setText(data.getName());
        holder.mTvFreeProductPrice.setText("￥" + String.format("%.2f", data.getPrice()));

        //一元购商品点击购买的时候，直接跳到购物车页面
        //一元购商品前端限制一次点击只能买一件
        holder.mTvFreeProductBuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ProductCart productCart = new ProductCart(null, data.getCategoryId(), data.getId(), FreeProductActivity.chooseDate);
                //设置商品属性添加到本地购物车
                productCart.setNum(1);
                productCart.setImage(data.getImage());
                productCart.setProductId(data.getId());
                productCart.setName(data.getName());
                productCart.setProductId(data.getId());
                productCart.setPrice(data.getPrice());
                productCart.setProductClassId(data.getProductClassId());
                productCart.setTotalPrice(data.getPrice());
                mCommonCart.productCartDao.insertOrReplace(productCart);

                //发送全局购物车信息更新事件，并跳转到购物车页面
                mCommonCart.totalNum += 1;
                mCommonCart.totalPrice += data.getPrice();
                mCommonCart.event.setTotalNum(mCommonCart.totalNum);
                mCommonCart.event.setTotalPrice(mCommonCart.totalPrice);
                EventBus.getDefault().post(mCommonCart.event);
                Intent intent = new Intent(mContext, UserCartActivity.class);
                mContext.startActivity(intent);
            }
        });

        return convertView;
    }

    static class ViewHolder {
        @Bind(R.id.iv_free_product)
        ImageView mIvFreeProduct;
        @Bind(R.id.tv_free_product_name)
        TextView mTvFreeProductName;
        @Bind(R.id.tv_free_product_price)
        TextView mTvFreeProductPrice;
        @Bind(R.id.tv_free_product_buy)
        TextView mTvFreeProductBuy;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
